package TESTS;

import java.util.Comparator;
import java.util.HashMap;
import java.util.SortedMap;
import java.util.*;


public class BTTest {
	FileHandler fh;
	PeerData peerData;
	
	public BTTest() {
		fh = new FileHandler("testFile.txt", 4200, 500);
		peerData = new PeerData();
	}
	
	public boolean testPeerData(){
		int[] kNghbr;
		System.out.println("Testing PeerData");
		
		peerData.testConfig1();
		kNghbr = peerData.topKNeighbors(2);
		if(kNghbr[0] == 0 && kNghbr[1] == 1)
			System.out.println("Test 1 Pass");
		else
			System.out.println("Test 1 FAIL");
		
		peerData.testConfig2();
		kNghbr = peerData.topKNeighbors(2);
		if(kNghbr[0] == 3 && kNghbr[1] == 4)
			System.out.println("Test 2 Pass");
		else
			System.out.println("Test 2 FAIL");
		
		kNghbr = peerData.topKNeighbors(1);
		if(kNghbr[0] == 4)
			System.out.println("Test 3 Pass");
		else
			System.out.println("Test 3 FAIL");
		
		peerData.testConfig4();
		kNghbr = peerData.topKNeighbors(4);
		if(peerData.containsInFirstK(kNghbr, 0, 3) && peerData.containsInFirstK(kNghbr, 2, 3) && peerData.containsInFirstK(kNghbr, 3, 3))
			System.out.println("Test 4 Pass");
		else
			System.out.println("Test 4 FAIL");
		
		peerData.testOptUnchoke1();
		
		return true;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BTTest testerObj = new BTTest();
		
		
		System.out.println(testerObj.testPeerData());
		
		
		/*String str = "This is a test statement";
		byte[] buffer = new byte[500];
		buffer = str.getBytes();
		
		testerObj.fh.setPiece(buffer, 8);
		testerObj.fh.setPiece(buffer, 0);
		buffer = testerObj.fh.getPiece(8);
		
		System.out.println(buffer.length);
		System.out.println(new String(buffer));
		testerObj.fh.close(); */
	}

}


class ValueComparator implements Comparator {

  Map base;
  public ValueComparator(Map base) {
      this.base = base;
  }

  public int compare(Object a, Object b) {

    if((Double)base.get(a) < (Double)base.get(b)) {
      return 1;
    } else if((Double)base.get(a) == (Double)base.get(b)) {
      return 0;
    } else {
      return -1;
    }
  }
}

